﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Windows.Media.Imaging;
using VisualWip.Core;
using VisualWip.Core.Interfaces;
using VisualWip.Core.Utils;

namespace VisualWip.PresentationModel
{
    public class BoardViewModel : INotifyPropertyChanged
    {
        private readonly Application _application;
        private readonly IBoardRecorder _boardRecorder;
        private readonly IZoomManager _zoomManager;

        public BoardViewModel(Application application, IZoomManager zoomManager, IBoardRecorder boardRecorder)
        {
            _application = application;
            _zoomManager = zoomManager;
            _boardRecorder = boardRecorder;
            _application.PropertyChanged += ApplicationPropertyChanged;
            _application.RefreshDone += ApplicationRefreshDone;
            _application.ResetBoard += ApplicationResetBoard;
            ColumnViewModels = new ObservableCollection<ColumnViewModelBase>();
        }

        private void ApplicationResetBoard(object sender, EventArgs e)
        {
            OnResetBoard();
        }

        public IZoomManager ZoomManager
        {
            get { return _zoomManager; }
        }

        public ObservableCollection<ColumnViewModelBase> ColumnViewModels { get; private set; }

        public bool IsDesignMode
        {
            get { return _application.IsDesignMode; }
        }

        public double Scale
        {
            get { return _application.ZoomLevel/100D; }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        public event EventHandler RecordBoard;

        protected virtual void OnRecordBoard()
        {
            if (RecordBoard != null)
                RecordBoard(this, EventArgs.Empty);
        }

        public event EventHandler ResetBoard;

        protected virtual  void OnResetBoard()
        {
            if (ResetBoard != null)
                ResetBoard(this, EventArgs.Empty);
        }

        private void ApplicationRefreshDone(object sender, EventArgs e)
        {
            if (_application.IsAutoRecordOn && _boardRecorder.CanRecord)
                OnRecordBoard();
        }

        private void ApplicationPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == PropertyHelper.Name(() => _application.IsDesignMode))
            {
                OnPropertyChanged(this, () => IsDesignMode);
            }
            if (e.PropertyName == PropertyHelper.Name(() => _application.ZoomLevel))
            {
                OnPropertyChanged(this, () => Scale);
            }
        }

        protected void OnPropertyChanged<T>(object sender, Expression<Func<T>> property)
        {
            PropertyHelper.OnPropertyChanged(sender, PropertyChanged, property);
        }

        public void SaveRecording(BitmapSource screenRecording)
        {
            _boardRecorder.SaveRecording(screenRecording);
        }
    }
}
